/*
 * Copyright (C) 2006-2015
 * 版权所有者为北京捷越联合信息咨询有限公司。本系统是商用软件,未经授权擅自复制或传播本程序的部分或全部将是非法的。
 * @title: ESBClient.java
 * @package com.jy.modules.cims.zz
 * @author
 * @date 2015-2-7 下午5:41:47
 * @version v1.00
 * @description: TODO(用一句话描述该文件做什么)
 */

package com.jy.modules.cims.interact;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.exception.InteractException;

/**
 * @classname: ESBClient
 * @description: http请求客户端
 */

public class CimsHttpClient implements IClient {

    private static final Logger logger = LoggerFactory.getLogger(CimsHttpClient.class);

    private CimsHttpClient() {
    }

    public static IClient getInstance() {
        return new CimsHttpClient();
    }

    /**
     * 发送请求
     * 
     * @title: sendMsg
     * @author
     * @description:
     * @date 2015-2-9 上午9:56:44
     * @param url url地址
     * @param xml 请求报文
     * @return
     * @throws Exception
     * @throws
     */
    public String sendMsg(String url, String xml) throws InteractException {
        StringBuffer sbf = new StringBuffer();
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "text/html;charset=UTF-8");
//            logger.info("发送报文："+xml);
            StringEntity stringEntry = new StringEntity("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + xml, HTTP.UTF_8);
            stringEntry.setContentType("text/xml; charset=utf-8");
            stringEntry.setContentEncoding("utf-8");
            httpPost.setEntity(stringEntry);
            
            HttpResponse httpResponse;
            // HttpClient
            httpResponse = closeableHttpClient.execute(httpPost);
          
            // 状态...状态不为200时，返回标识00.
            int status = httpResponse.getStatusLine().getStatusCode();
           
            if (status != HttpStatus.SC_OK) {
                throw new InteractException(CimsExceptionConstant.CIMS_HTTP_ERROR);
            }
            // 获取响应消息实体
            InputStream is = httpResponse.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sbf.append(line);
            }
//            logger.info("返回报文："+sbf.toString());
            return sbf.toString();
        }
        catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                closeableHttpClient.close();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return sbf.toString();
    }

}
